package com.educhains.web.util;

import java.util.Random;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * @author YuRun
 * @since 2017年8月21日
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * Verify the email
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        String regex = "[a-zA-Z0-9_-]+@\\w+-*\\w+\\.[a-z]+(\\.[a-z]+)?";
        Pattern p = Pattern.compile(regex);
        return p.matcher(email).matches();
    }

    /**
     * Generate the random number
     *
     * @param length
     * @return
     */
    public static String generateRandomNumber(int length) {
        String base = "0123456789";
        return generateRandomString(base, length);
    }

    /**
     * Generate the random String
     *
     * @param length
     * @return
     */
    public static String generateRandomString(int length) {
        String base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        return generateRandomString(base, length);
    }

    /**
     * Generate UUID
     *
     * @return
     */
    public static String generateUUID() {
        return remove(UUID.randomUUID().toString(), '-');
    }


    private static String generateRandomString(String base, int length) {
        Random random = new Random(System.currentTimeMillis());
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
